<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""DTD/xhtml1-strict.dtd">
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@taglib uri="/WEB-INF/fmt.tld" prefix="fmt"%>
<%@taglib uri="/WEB-INF/el.tld" prefix="el"%>
<%@taglib uri="/WEB-INF/fn.tld" prefix="fn"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<c:import url="/includes/css_inc.jsp"/>
		<c:import url="/includes/js_inc.jsp"/>
		<script>
			$(function(){
				$("#save").button({
		            icons: {
		                primary: "ui-icon-disk"
		            }
				}).click(function(){
					$("#allocateDisPlay").removeAttr("disabled");
		        	dateFormat(document.getElementById("incomeDate"));
					if (validateEmpty()){
						disableField(["save","confirm"]);
						$("#budgetYear").removeAttr("disabled");
						$("#save").addClass("ui-button-disabled ui-state-disabled");
						$("#confirm").addClass("ui-button-disabled ui-state-disabled");
						$("#saveForm").submit();
				 	}else{
				 		$("#allocateDisPlay").attr("disabled","disabled");
				 	}
				});
				$("#edit").button({
		            icons: {
		                primary: "ui-icon-disk"
		            }
				}).click(function(){
					$("#budgetYear").removeAttr("disabled");
					$("#job").val("edit");
					$("#saveForm").submit();
				});

				$("#btnSearch").button({
		            icons: {
		                primary: "ui-icon-search"
		            }
		        });
				
				$("#back").button({
		            icons: {
		                primary: "ui-icon-arrowreturnthick-1-w"
		            }
		        }).click(function() {
		        	$("#job").val("back");
		        	$("#saveForm").submit();
		        });
				$("#confirm").button({
		            icons: {
		                primary: "ui-icon-circle-check"
		            }
		        }).click(function() {
					var remain = document.getElementById("remain").value;
					var incomeAmount = document.getElementById("incomeAmount").value;
					if(toFloat(incomeAmount) > toFloat(remain)){
						alert("จำนวนเงินรับคืนได้ไม่เกิน "+addCommas2D(remain)+" กรุณาตรวจสอบ.");
						document.getElementById("incomeAmount").value = "";
						document.getElementById("incomeAmount").focus();
					} else {
						if (validateEmpty()){
							if(confirm('คุณต้องการยืนยันข้อมูล?')==true){
			        			$("#job").val("confirm");
								$("#budgetYear").removeAttr("disabled");
								disableField(["save","confirm"]);
								$("#save").addClass("ui-button-disabled ui-state-disabled");
								$("#confirm").addClass("ui-button-disabled ui-state-disabled");
			        			$("#saveForm").submit();
							}
						}
					}
					
		        });
				
				$("#budgetYear").change(function(){
					if($("#budgetYear").val() != "" && $("#budgetYear").val() != null){
						var dhtml = new DHTML();
						$('#refNo')
						.find('option')
					    .remove()
					    .end()
					    .append('<option value=""></option>')
					    .val('')
						;
						
						$('#venderCode')
						.find('option')
					    .remove()
					    .end()
					    .append('<option value=""></option>')
					    .val('')
						;
						
						$('#allocateCode')
						.find('option')
					    .remove()
					    .end()
					    .append('<option value=""></option>')
					    .val('')
						;
						
						$("#venderDisplay").val("");
						$("#allocateCode").val("");
						$('#spanRefNo input').val("");
						$('#spanVender input').val("");
						$('#spanallocateCode input').val("");
						$.ajax({
							type: "post",
							url: "/provacc/acc67",
							dataType: "json",
							async: false,
							cache: false,
							data: {
								app: "ajax",
								job: "getRefNo",
								budgetYear: $("#budgetYear").val(),
								provinceId: $("#provinceId").val(),
							},
							success: function(data) {
								if(data.length > 0){
									for(var i=0;i<data.length;i++){
										dhtml.createElement(document.getElementById("refNo"), ["option","value="+data[i].refNo], data[i].refNo);
									}
								}
							}
						});
					}
				});

				$("#provinceId").change(function(){
					$('#refNo')
					.find('option')
				    .remove()
				    .end()
				    .append('<option value=""></option>')
				    .val('')
					;
					
					$('#venderCode')
					.find('option')
				    .remove()
				    .end()
				    .append('<option value=""></option>')
				    .val('')
					;
					
					$('#allocateCode')
					.find('option')
				    .remove()
				    .end()
				    .append('<option value=""></option>')
				    .val('')
					;
					
					//$("#venderDisplay").val("");
					$("#allocateCode").val("");
					$('#spanRefNo input').val("");
					$('#spanVender input').val("");
					$('#spanAllocateCode input').val("");
				});
				$( "#refNo" ).combobox({ selected: function(event, ui) {
					if(($("#refNo").val() != "" && $("#refNo").val() != null) 
						&& ($("#provinceId").val() != "" && $("#provinceId").val() != null) 
						&& ($("#budgetYear").val() != "" && $("#budgetYear").val() != null)){
						var dhtml = new DHTML();
						var refNo = $("#refNo").val();

						$('#venderCode')
						.find('option')
					    .remove()
					    .end()
					    .append('<option value=""></option>')
					    .val('')
						;
						
						$('#allocateCode')
						.find('option')
					    .remove()
					    .end()
					    .append('<option value=""></option>')
					    .val('')
						;
						$('#spanVender input').val("");
						$('#spanAllocateCode input').val("");
						
						$.ajax({
							type: "post",
							url: "/provacc/acc67",
							dataType: "json",
							data: {
								app: "ajax",
								job: "getVenderCodeByRefNo",
								refNo: encodeURIComponent(refNo),
								provinceId: $("#provinceId").val(),
								budgetYear: $("#budgetYear").val()
							},
							success: function(data) {
								if(data[0].result == "1"){
									for(var i=0;i<data.length;i++){
										if (data[i].venderAccount != null && data[i].venderAccount != "") {
											dhtml.createElement(document.getElementById("venderCode"), ["option","value="+data[i].venderCode], data[i].venderAccount + " - " +data[i].venderName);
										} else {
											dhtml.createElement(document.getElementById("venderCode"), ["option","value="+data[i].venderCode], data[i].venderName);
										}
									}
								} 
							}
						});
					}else{
						alert("กรุณากรอกข้อมูลให้ครบ");
					}
				}});
				$( "#venderCode" ).combobox({ selected: function(event, ui) {
					var dhtml = new DHTML();
					var refNo = $("#refNo").val();
					$('#allocateCode')
					.find('option')
				    .remove()
				    .end()
				    .append('<option value=""></option>')
				    .val('')
					;

					$("#allocateCode").val("");
					$('#spanAllocateCode input').val("");
					$.ajax({
						type: "post",
						url: "/provacc/acc67",
						dataType: "json",
						data: {
							app: "ajax",
							job: "getAllocateByRefNo",
							refNo: encodeURIComponent(refNo),
							venderCode: $("#venderCode").val(),
							provinceId: $("#provinceId").val(),
							budgetYear: $("#budgetYear").val()
						},
						success: function(data) {
							if(data[0].result == "1"){
								for(var i=0;i<data.length;i++){
										dhtml.createElement(document.getElementById("allocateCode"), ["option","value="+data[i].allocateCode], data[i].allocateCode + " - " + data[i].allocateName);
								}
							} 
						}
					});
				}});
				$( "#allocateCode" ).combobox({ selected: function(event, ui) {
					var dhtml = new DHTML();
					var refNo = $("#refNo").val();
					$.ajax({
						type: "post",
						url: "/provacc/acc67",
						dataType: "json",
						data: {
							app: "ajax",
							job: "getAllocateCodeById",
							refNo: encodeURIComponent(refNo),
							venderCode: $("#venderCode").val(),
							provinceId: $("#provinceId").val(),
							budgetYear: $("#budgetYear").val(),
							allocateCode: $("#allocateCode").val()
						},
						success: function(data) {
							if(data[0].result == "1"){
								$("#catId").val(data[0].catId);
								$("#remain").val(data[0].remain);
							}
						}
					});
				}});
				$('#spanRefNo input').css('width','150px');
				$('#spanVender input').css('width','500px');
				$('#spanAllocateCode input').css('width','500px');
		        thaiDatepicker("incomeDate","#","1");
			});
			function searchRefNo(){
				if(($("#refNo").val() != "" && $("#refNo").val() != null) 
					 && ($("#provinceId").val() != "" && $("#provinceId").val() != null) 
					 	&& ($("#budgetYear").val() != "" && $("#budgetYear").val() != null)){
					$.ajax({
						type: "post",
						url: "/provacc/acc67",
						dataType: "json",
						async: false,
						cache: false,
						data: {
							app: "ajax",
							job: "getAllocateCodeById",
							refNo: encodeURIComponent($("#refNo").val()),
							provinceId: $("#provinceId").val(),
							budgetYear: $("#budgetYear").val()
						},
						success: function(data) {
							if(data[0].result == "1"){
								for(var i=0;i<data.length;i++){
									$("#allocateDisPlay").val(data[i].code + " - " + data[i].name);
									$("#allocateCode").val(data[i].code);
									$("#catId").val(data[i].catId);
									$("#remain").val(data[i].remain);
								}
							}
						}
					});
					$.ajax({
						type: "post",
						url: "/provacc/acc67",
						dataType: "json",
						async: false,
						cache: false,
						data: {
							app: "ajax",
							job: "getVenderCodeByRefNo",
							refNo: encodeURIComponent($("#refNo").val()),
							provinceId: $("#provinceId").val()
						},
						success: function(data) {
							if(data[0].result == "1"){
								$("#venderDisplay").val(data[0].venderName);
							} else {
								$("#venderDisplay").val("");
							}
						}
					});
				}else{
					alert("กรุณากรอกข้อมูลให้ครบ");
				}
			}
			function checkAmount(value){
				var remain = document.getElementById("remain").value;
				if(toFloat(value) > toFloat(remain)){
					alert("จำนวนเงินรับคืนได้ไม่เกิน "+addCommas2D(remain)+" กรุณาตรวจสอบ.");
					document.getElementById("incomeAmount").value = "";
					document.getElementById("incomeAmount").focus();
				}
			}
		</script>
	</head>
	<body>
		<c:set var="auth" value="${el:getMapValue(menuAuth, 'ACC60204')}"/>
		<c:import url="/apps/msg.jsp"/>
		<form class="acc67-form" name="saveForm" id="saveForm" action="${pageContext.request.contextPath}/acc6" method="post">
		<input type="hidden" name="app" id="app" value="ACC60204"/>
		<input type="hidden" name="job" id="job" value="save"/>
		<input type="hidden" name="docNo" id="docNo" value="${incomeBean.docNo}"/>
		<input type="hidden" name="remain" id="remain" value="${remain}"/>
		<input type="hidden" name="catId" id="catId" value="${catId}"/>
		<c:if  test="${incomeBean.incomeStatus == 'Y' || userInfo.areaLevel=='2'}" var="isDisable"/>
			<table width="100%" class="ui-widget ui-widget-content" border="0">
				<tr>
					<th align="left" class="acc67-header ui-widget-header" colspan="2"><b>${screenCode.ACC60204}</b></th>
				</tr>
				<tr>
					<td width="20%" class="bold">จังหวัด : </td>
					<td>	
						<c:if test="${userInfo.areaLevel =='3'}">
							<c:forEach var="data" items="${userInfo.listUserProvice}">
								${data.provinceName}
								<input type="hidden" name="provinceId" id="provinceId" value="${data.provinceId}">
							</c:forEach>
						</c:if>
						<c:if test="${userInfo.areaLevel=='1' || userInfo.areaLevel=='2'}">
							<c:if test="${isEdit == 'N'}">
								<select name="provinceId" id="provinceId" ${isDisable?'disabled':''}>
									<option value="">----- เลือก -----</option>
									<c:forEach var="data" items="${userInfo.listUserProvice}">
										<c:if test="${data.provinceId == provinceId}" var="provinceSeleted"/>
										<option value="${data.provinceId}" ${provinceSeleted?'selected':''}>${data.provinceName}</option>
									</c:forEach>
								</select>
								<span id="$provinceId" class="red">*</span>
							</c:if>
							<c:if test="${isEdit == 'Y' || isEdit == 'V'}">
								${provinceName}
								<input type="hidden" name="provinceId" id="provinceId" value="${provinceId}">
							</c:if>
						</c:if>
					</td>
				</tr>
				<tr>
					<td width="20%" class="bold">เลขที่เอกสาร : </td>
					<td>${incomeBean.docNo}</td>
				</tr>
				<tr>
					<td class="bold">ปีงบประมาณ : </td>
					<td>
						<select name="budgetYear" id="budgetYear" ${isDisable || isEdit=='Y'?'disabled':''}>
							<option value="">----- เลือก -----</option>
							<c:if test="${!empty listBudgetYear}">
								<c:forEach var="data" items="${listBudgetYear}" varStatus="x">
									<c:if var="selected" test="${data.BUDGET_YEAR == incomeBean.budgetYear}"/>
									<option value="${data.BUDGET_YEAR}" ${selected ? 'selected' : ''}>${data.BUDGET_YEAR}</option>
								</c:forEach>
							</c:if>
						</select><span id="$budgetYear" class="red">*</span>
					</td>
				</tr>
				<tr>
					<td class="bold">เลขที่เอกสารอ้างอิง : </td>
					<td>
						<c:if test="${isDisable}">
							<input type="text" value="${incomeBean.refNo}" disabled="disabled"/>
							<input type="hidden" name="refNo" value="${incomeBean.refNo}"/>
						</c:if>
						<c:if test="${!isDisable}">
							<c:if test="${isEdit=='Y'}">
								<input type="text" value="${incomeBean.refNo}" disabled="disabled"/>
								<input type="hidden" name="refNo" value="${incomeBean.refNo}"/>
							</c:if>
							<c:if test="${isEdit=='N'}">
								<span id="spanRefNo" ${isDisable?'disabled':''}>
									<select name="refNo" id="refNo">
									</select>
								</span>
							</c:if>
						</c:if>
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<span id="$refNo" class="red">*</span>
					</td>
				</tr>
				<tr>
					<td class="bold">หน่วยบริการ/คู่สัญญา : </td>
					<td>
						<c:if test="${isEdit=='Y' || isEdit=='V'}">
							<input type="text" value="${empty venderAccount ? '' : venderAccount} ${venderDisplay}" size="50"  disabled="disabled"/>
							<input type="hidden" name="venderCode" value="${venderCode}"/>
						</c:if>
						<c:if test="${isEdit=='N'}">
							<span id="spanVender" ${isDisable?'disabled':''}>
								<select name="venderCode" id="venderCode">
								</select>
							</span>
						</c:if>
					</td>
				</tr>
				<tr>
					<td class="bold">กองทุนย่อยเฉพาะด้าน : </td>
					<td>
						<c:if test="${isEdit=='Y' || isEdit=='V'}">
							<input type="text" name="allocateDisPlay" id="allocateDisPlay" disabled="disabled" size="50"  value="${allocateDisplay}">
							<input type="hidden" name="allocateCode" value="${incomeBean.allocateCode}">
						</c:if>
						<c:if test="${isEdit=='N'}">
							<span id="spanAllocateCode" ${isDisable?'disabled':''}>
								<select name="allocateCode" id="allocateCode">
								</select>
							</span>
							<span id="#allocateCode" class="red">*</span>
						</c:if>
					</td>
				</tr>
				<tr>
					<td class="bold">วันที่ทำรายการ : </td>
					<td>
						<c:if test="${isEdit == 'N'}"><!-- add -->
							${now}
						</c:if>
						<c:if test="${isEdit == 'Y'}"><!-- update -->
							${el:dateTh(incomeBean.createdDate)}
						</c:if>
						<c:if test="${isEdit == 'V'}"><!-- view -->
							${el:dateTh(incomeBean.createdDate)}
						</c:if>
					</td>
				</tr>
				<tr>
					<td class="bold">วันที่รับเงิน : </td>
					<td>
						<input type="text" name="incomeDate" id="incomeDate" size="12" maxlength="10" onkeypress="return numberAndSlash(event);" onblur="dateFormat(this);" value="${el:dateTh(incomeBean.incomeDate)}" ${isDisable?'disabled':''}>
						<span id="#incomeDate" class="red">*</span>
					</td>
				</tr>
				<tr>
					<td class="bold">จำนวนเงิน : </td>
					<fmt:formatNumber var="incomeAmount" pattern="#,##0.00" value="${incomeBean.incomeAmount}"/>
					<td>
						<input type="text" name="incomeAmount" id="incomeAmount" ${isDisable?'disabled':''} onkeypress="return numberAndDot(event);" value="${incomeAmount}" onblur="this.value = addCommas2D(this.value); checkAmount(this.value);"/>
						<span id="#incomeAmount" class="red">*</span>
					</td>
				</tr>
				<tr>
					<td class="bold" valign="top">คำอธิบายรายการ : </td>
					<td>
						<textarea cols="40" rows="5" name="incomeDesc" id="incomeDesc" ${isDisable?'disabled':''}>${incomeBean.incomeDesc}</textarea>
					</td>
				</tr>
				<tr>
					<td colspan="2" align="center">
					<c:if test="${!isDisable}">
						<c:if test="${isEdit == 'N'}">
						<c:if test="${fn:contains(auth, 'I')}">
							<button type="button" name="save" id="save" class="acc67-button">บันทึกข้อมูล</button>
						</c:if>
						</c:if>
						<c:if test="${isEdit == 'Y'}">
							<c:if test="${fn:contains(auth, 'U')}">
								<button type="button" name="edit" id="edit"  class="acc67-button">บันทึกข้อมูล</button>
								<button type="button" name="confirm" id="confirm"  class="acc67-button">ยืนยันข้อมูล</button>
							</c:if>
						</c:if>
					</c:if>
						<button type="button" name="back" id="back"  class="acc67-button">ย้อนกลับ</button>
					</td> 
				</tr>
			</table>
		</form>
	</body>
</html>